package com.qfedu.fmmall.controller;

import com.github.wxpay.sdk.WXPay;
import com.qfedu.fmmall.common.status.ResStatus;
import com.qfedu.fmmall.common.vo.ResultVO;
import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.service.OrdersService;
import com.qfedu.fmmall.service.config.MyPayConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : yanBin
 * @date :2021/12/21 9:11
 */
@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "订单页面所需接口", tags = "订单管理")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/add")
    @ApiOperation("添加订单接口")
    public ResultVO addOrders(String cids, @RequestBody Orders orders ,@RequestHeader("token")String token) {
        try {
            Map<String, String> map = ordersService.addOrders(cids, orders);
            WXPay wxPay = new WXPay(new MyPayConfig());
            Map<String, String> data = new HashMap();
            data.put("body", map.get("productNames"));           //商品描述
            data.put("out_trade_no", map.get("orderId"));    //使用当前用户订单编号作为当前支付的交易号
            data.put("fee_type", "CNY");          //支付币种
            //data.put("total_fee", String.valueOf(orders.getActualAmount()*100));           //支付金额
            data.put("total_fee", "1");           //支付金额
            data.put("trade_type", "NATIVE");      //交易类型
            data.put("notify_url", "/pay/callback"); //设置支付完成时的回调接口

            //发送请求，获取响应
            Map<String, String> resp = wxPay.unifiedOrder(data);
            map.put("payUrl",resp.get("code_url"));
            return new ResultVO(ResStatus.OK.ordinal(), "添加订单成功",map);
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO.ordinal(), "添加订单失败", null);
        }
    }

    @GetMapping("/status/{orderId}")
    @ApiOperation("查询订单接口状态")
    public ResultVO getOrderById(@PathVariable("orderId") String orderId,@RequestHeader("token")String token) {
        return ordersService.getOrderById(orderId);
    }

    @GetMapping("/list")
    @ApiOperation("用户查询订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(dataType = "String",name = "status",value = "订单状态",required = false),
            @ApiImplicitParam(dataType = "int",name = "page",value = "页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit",value = "每一页多少条数据",required = true),
            @ApiImplicitParam(dataType = "String",name = "token",value = "令牌字符串",required = true)})
    public ResultVO listOrders(String userId, String status, int page, int limit,@RequestHeader("token")String token) {
        return ordersService.listOrders(userId,status,page,limit);
    }

}
